/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.nc.kyiv.yellow.sociallibrary.DataSet;

import com.nc.kyiv.yellow.sociallibrary.DataSetDAO.DashboardDataSet;
import com.nc.kyiv.yellow.sociallibrary.POJO.Book;
import com.nc.kyiv.yellow.sociallibrary.helper.MapBuilder;
import com.nc.kyiv.yellow.sociallibrary.similarUse.AbstractDataSet;
import com.nc.kyiv.yellow.sociallibrary.similarUse.CommonClassesInterface;
import java.util.List;
import java.util.Map;

/**
 *
 * @author Alena
 */
public class GetPublishedDashboard implements CommonClassesInterface {

    private String userId;

    public GetPublishedDashboard(String userID) {
        this.userId = userID;
    }
    private String[] mapKeys = {"pageNumber", "booksNumber", "filterOneValue", "filterTwoValue"};
    private MapBuilder helper = new MapBuilder();
    private String resultString = null;

    @Override
    public String execute(String parameters) throws Exception {
        Map postParameters = helper.stringToMap(parameters, mapKeys, helper.getRequestSplitter());
        postParameters.put("publishedBy", "1");
        postParameters.put("userId", userId);

        if (postParameters != null) {
            try {
                AbstractDataSet dashboardSet = new DashboardDataSet();
                List<Book> dashboard = (List<Book>) dashboardSet.getDataSet(postParameters);

                System.out.println(dashboard);
                
                ResultStringOfBooks resultStringOfBooks = new ResultStringOfBooks();
                resultString = resultStringOfBooks.getResultString(dashboard, helper);

                System.out.println(resultString);
                
                if (resultString.length() > 5) {
                    resultString = resultString.substring(0, resultString.length() - 5);
                } else {
                    return "failed";
                }
            } catch (Exception ex) {
                System.out.println("for debugging" + ex.getMessage());
                return "failed";
            }
            return resultString;

        } else {
            return "failed";
        }
    }
}
